home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / unix / process.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  4.1 KB  |  153 lines

  1. //
  2. //
  3. //    Processes work as hungry puppies, banging on the scheduler
  4. //    until a ready job is available or until someone bangs on the
  5. //    process asking it to do something "out of band" (like pausing
  6. //    or dying).
  7. //
  8.  
  9. extern Process *sysproc;
  10.  
  11. class Invocation;
  12.  
  13. #define NUMPROCS    16
  14.  
  15. class Process    : public Object     {
  16. #if (sun && THREAD_HAS_INTERRUPTIBLE_FIELD)
  17.     int    p_interruptible;
  18. #endif
  19. #ifdef vax
  20.     // WARNING: The offset of p_interruptible is known to swtch().
  21.     int    p_interruptible;
  22. #endif vax
  23.     char    *p_name;            // my name
  24.     int    p_id;                // my id
  25.     int    p_pid;                // system pid
  26.     int    p_ppid;                // system ppid
  27. protected:
  28.     int    p_state;            // whats up
  29.     int     p_flags;            // long term process stuff
  30.     int    p_request;            // stop spinning on this
  31.     Thread  *p_schedthread;            // where to swtch back to 
  32.     Thread    *p_thread;            // currently running thread
  33. #define NEWPROCESS
  34.     virtual void    p_fork();            // take care of the fork
  35.     virtual void    p_wait();            // where we spin
  36.     virtual void    p_pause();            // internal ::pause
  37.     virtual void    p_runchild(int *spinflag);    // copy stack and go
  38. public:
  39.     Process(int ptag, int id);
  40.     Process();
  41.     Process(char* name, int id, int delayedfork = 0); // fork in ctor
  42.     virtual ~Process();
  43.     // virtual newprocess function.  After creation of the first thisproc,
  44.     // raw process constructor is not used.  thisproc->newprocess() is
  45.     // used instead.  User can assign his own derivation of Process to
  46.     // thisproc in Main::init().
  47.     virtual Process* newprocess(char* name,int id);    
  48.     virtual int    request(int req);
  49.     virtual void    park();            // os pause
  50.     virtual void    drive();        // resume
  51.     virtual int    invoke();
  52.     int    state()
  53.         { return p_state; }
  54.     void     setstate(int st)
  55.         { p_state = st; }
  56.     int     flags()
  57.         { return p_flags; }
  58.     inline int isroot();
  59.     int    id()                // scheduler id
  60.         {return p_id; }    
  61.     int    pid()            // unix id
  62.         { return p_pid; }
  63.     int     ppid()            // unix parent id
  64.         { return p_ppid; }
  65.     char *    name()
  66.         { return p_name; }
  67.     Thread    *schedthread()
  68.         { return p_schedthread; }
  69.     Thread    *runningthread()
  70.         { return p_thread; }
  71. #if (sun && THREAD_HAS_INTERRUPTIBLE_FIELD)
  72.     int    interruptible()
  73.         { return p_interruptible; }
  74.     inline int disable_interrupts();
  75.     void enable_interrupts()
  76.         { p_interruptible = 1; }
  77. #endif sun
  78. #ifdef vax
  79.     int    interruptible()
  80.         { return p_interruptible; }
  81.     inline int disable_interrupts();
  82.     void enable_interrupts()
  83.         { p_interruptible = 1; }
  84. #endif vax
  85.     virtual void print(ostream& = cout);
  86.     virtual void    error(char *s);
  87. };
  88.  
  89.     
  90. #define R_NULL        0x00    /* nothing is happening */
  91. #define R_WAKEUP    0x01    /* wakeup wherever you are */
  92. #define R_PARK        0x02    /* put yourself to sleep in the OS */
  93. #define R_DIE        0x04    /* the nice way to kill a process */
  94. #define R_RETURN    0x08    /* relinquish control */
  95. /*
  96.  * We don't need R_DIE and R_RETURN since the process wait routine
  97.  * only understands how to R_RETURN from its scheduling loop.  Where
  98.  * it returns to is completely determined by how it was invoked.
  99.  * If it was invoked from runchild, then we will return to there
  100.  * and get nuked with a (delete this).  If we were called via runrun
  101.  * from the Main constructor, then we will return to single threaded
  102.  * control.
  103.  */
  104.  
  105. #define S_RUN        0x01    /* running */
  106. #define S_WAIT        0x02    /* unused.  waiting to be used in spinloop */
  107. #define S_SLEEP        0x04    /* sleeping on an event */
  108. #define S_OSPAUSE    0x08    /* WAITing or SLEEPing via OS pause */
  109. #define S_FREE        (S_WAIT|S_OSPAUSE)
  110. #define S_ZOMBIE    0x10
  111. #define S_EXITING    0x20
  112. #define S_DELAYEDFORK    0x40    /* constructed, but not yet forked */
  113. #define S_FORKING    0x80    /* as we speak... */
  114. #define S_ERROR        0x100
  115. #define S_REAPED    0x200    /* already cleaned up */
  116. #define S_REAPING    0x400    /* parent process cleaning up */
  117.  
  118.  
  119. //
  120. // special proc tags for flags field
  121. //
  122. #define P_ROOT        0x01
  123.  
  124. inline int Process::isroot()
  125. {
  126.     return (p_flags&P_ROOT);
  127. }
  128.  
  129. #if (sun && THREAD_HAS_INTERRUPTIBLE_FIELD)
  130. inline int Process::disable_interrupts()
  131. {
  132.     if (p_interruptible)    {
  133.         p_interruptible = 0;
  134.         return 1;
  135.  
  136.     } else
  137.         return 0;
  138. }
  139. #endif sun
  140. #ifdef vax
  141. inline int Process::disable_interrupts()
  142. {
  143.     if (p_interruptible)    {
  144.         p_interruptible = 0;
  145.         return 1;
  146.  
  147.     } else
  148.         return 0;
  149. }
  150. #endif vax
  151.  
  152. extern private_t Process    *thisproc;    // private to each proc
  153.